//JavaScript代码区域
layui.use('element', function () {
    var element = layui.element;
});
layui.use(['table', 'form'], function () {

    var table = layui.table;
    var form = layui.form;



    form.verify({
        count:function(){

            if (!checkCount()) {
                return "请输入正整数";
            }

        },
        carNum:function(){
            if (!checkCarNum())
                return "请输入正确的车牌号";

        }

    })


    var defaltCount = 3;
    var count = ($("#count").val() ==null || $("#count").val() =="") ? defaltCount:$("#count").val()
    var array = [];

    array.push({field: 'carNum', title: '车牌',width:170})
    array.push({field: 'dateTime', title: '过车时间',width:220})
    array.push({field: 'illegalPlace', title: '违法地点',width:220})

    for(var i=0;i<count;i++){
        array.push({field: 'passCarPlace'+i, title: '过车地点',width:220})
        array.push({field: 'passCarDate'+i, title: '过车时间',width:220})
    }

    tableRender(array)



    //第一个实例
    function tableRender(array){

        table.render({
            elem: '#tableList'
            , url: '/car/list'  //数据接口
            , method: "post"
            , contentType: "application/json"
            , async: true
            , page: false
            ,done: function (res, curr, count) {
                $("table").css("width", "100%");
            }
            , where: {
                count:($("#count").val() ==null || $("#count").val() =="") ? defaltCount:$("#count").val(),
                carNum:$("#carNum").val()
                //这里传参  向后台
            }
            , cols: [  array ]
            , parseData: function (res) {//将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code == 200?0:-1, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    // "count": res.data.total, //解析数据长度
                    "data": res.data //解析数据列表
                };
            }
        });
    }

    function checkCount(){
        var count = ($("#count").val() ==null || $("#count").val() =="") ? defaltCount:$("#count").val()

        if(count != null && count != ""){
            var re = /^[1-9]\d*$/
            if (!re.test(count)) {
                return false;
            }else{
                return true;
            }

        }else{
            return true;
        }

    }




   function checkCarNum(){
       var carNum = $("#carNum").val()
        if(carNum != null && carNum != "") {
            var re = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/
            if (!re.test(carNum)) {
                return false;
            }else{
                return true;
            }
        }else{
            return true;
        }
    }






    /*搜索*/
    $("#sousuo").on("click", function () {

        form.on('submit(search)', function (data) {

            array.splice(3,array.length-3)

            var c = ($("#count").val() ==null || $("#count").val() =="") ? defaltCount:$("#count").val()

            for(var i=0;i<c;i++){
                array.push({field: 'passCarPlace'+i, title: '过车地点',width:220})
                array.push({field: 'passCarDate'+i, title: '过车时间',width:220})
            }

            console.log("22->"+array)

            //执行重载
            tableRender(array)



            return false;//false：阻止表单跳转  true：表单跳转
        });
    })



    /*导出点击事件*/
    $("body").on("click", ".layui-btn.layui-btn-sm.dc", function () {

        $.post("/car/checkExportExcel",function(data){
            if(data.code != 200){
                layer.msg(data.msg)
            }else{
                window.location.href = "/car/exportExcel";
            }

        })



    })




    /*导入点击事件*/
    $("body").on("click", ".layui-btn.layui-btn-sm.dr", function () {
        if ($("#uploadFile").val() == "") {
            layer.msg("导入前请先选择文件！")
        } else {
            var formData = new FormData();
            formData.append("file", $("#uploadFile")[0].files[0]);

            var reg = /^.*\.(?:xls|xlsx)$/i;//文件名可以带空格
            if (!reg.test($("#uploadFile").val())) {//校验不通过
                layer.msg("请选择excel格式的文件!")
            } else {
                $.ajax({
                    url: "/car/import",
                    type: 'POST',
                    async: false,
                    data: formData,
                    // 告诉jQuery不要去处理发送的数据
                    processData: false,
                    // 告诉jQuery不要去设置Content-Type请求头
                    contentType: false,
                    beforeSend: function () {
                        layer.msg("正在导入！")
                    },
                    success: function (responseStr) {
                        if(responseStr.code == 200){
                            var file = $("#uploadFile");
                            $(file).val('');
                            table.reload('tableList');
                            layer.msg("导入成功！")
                        }else{
                            var file = $("#uploadFile");
                            $(file).val('');
                            layer.msg(responseStr.msg)
                        }



                    }
                });
            }
        }
    })

    /*模板下载点击事件*/
    $("body").on("click", ".layui-btn.layui-btn-sm.mbxz", function () {
        window.location.href = "/导入模板.xlsx"
    })










})


